package mall

import (
	"context"
	"fmt"
	"github.com/samber/lo"
	"private-domain-overseas-service/admin/business/api/internal/repository/mall"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmallsite"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MallSiteListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewMallSiteListLogic - 站点列表
func NewMallSiteListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MallSiteListLogic {
	return &MallSiteListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MallSiteListLogic) MallSiteList() (resp *types.MallSiteListResponse, err error) {
	// 创建实例
	mallSiteDao := mall.NewMallSiteDao(l.ctx, l.svcCtx)

	// 查询站点
	brandSiteConditions := make([]predicate.HzMallSite, 0)
	brandSiteConditions = append(brandSiteConditions, hzmallsite.DeleteAtIsNil())
	brandSites, err := mallSiteDao.MallSiteConditionsAll(brandSiteConditions)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerDataNotFoundError, fmt.Sprintf("查询站点列表失败%v", err))
	}

	// 组装数据
	list := lo.Map(brandSites, func(item *ent.HzMallSite, _ int) *types.MallSiteList {
		return &types.MallSiteList{
			Id:           int64(item.ID),
			MallSiteName: item.MallSiteName,
			MallSiteIcon: constant.MallGoodsImage + item.MallSiteIcon,
			MallSiteShow: item.MallSiteShow,
			MallSiteSort: item.MallSiteSort,
			CreatedAt:    time.Unix(item.CreatedAt, 0).Format(time.DateTime),
			UpdatedAt:    time.Unix(item.UpdateAt, 0).Format(time.DateTime),
		}
	})

	// 组装返回数据
	resp = &types.MallSiteListResponse{
		List: list,
	}

	return resp, nil
}
